<?php 
namespace Home\Controller;
use Think\Controller;
use Home\Tool\CarTool;
class FlowController extends Controller{
	public function add(){
		$model= D('Goods');
		$goodsinfo = $model->find(I('get.goods_id'));
		if(!$goodsinfo){
			$this->redirect('/');
			exit;
		}

		$car = CarTool::getIns();
		$car->add($goodsinfo['goods_id'],$goodsinfo['goods_name'],$goodsinfo['shop_price']);
		$this->assign('che',$car->items());
		$this->display('Goods/checkOut');
	}

	public function done(){
		$car = CarTool::getIns();
		$oi = M('ordinfo');
		$oi->create();
		$oi->ord_sn = $ord_sn = date('Ymd').rand(10000,99999);
		$oi->user_id = cookie('user_id')?cookie('user_id'):0;
		$oi->money = $car->calcMoney();
		$oi->ordtime = time();

		if($ordinfo_id = $oi->add()){
			$og = M('ordgoods');
			$data = array();
			foreach($car->items() as $k => $v){
				$row = array();
				$row['goods_id'] = $k;
				$row['goods_name'] = $v['goods_name'];
				$row['shop_price'] = $v['shop_price'];
				$row['goods_num'] = $v['num'];
				$row['ordinfo_id'] = $ordinfo_id;
				$data[] = $row;
			}
			if($og->addAll($data)){
				$this->assign('ord_sn',$ord_sn);
				$this->assign('money',$car->calcMoney());
				$this->display('Goods/done');
			}
		}
		
	}
}